package com.example.answer_staff;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.gaosai.staff.utils.CommonUtils;
import com.gaosai.staff.view.CustomFlowLayout;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class ListCampDetailsActivity  extends Activity{

	private LayoutInflater mInflater ;

	/**从新闻列表中取得活动的条止数据**/
	private String getActivityData;

	private DisplayImageOptions options; 

	private Button register_button;

	/**返回按钮**/
	private ImageView activity_detail_backbutton;

	/**活动详情中的图片*/
	private ImageView activity_detail_imageView ;

	/**活动详情标题*/
	private TextView activity_detail_title ;

	/**活动详情内容*/
	private TextView activity_detail_content ;

	/**活动详时间*/
	private TextView activity_detail_time ;

	/**活动详情日期*/
	private TextView activity_detail_date ;

	/**活动详情venune*/
	private TextView activity_detail_venue ;

	/**新闻详情中的标签的流式布局**/
	private CustomFlowLayout activity_detail_flowlayout ;

	private ImageView back_campain_details;
	private String str_list;

	//	private String subject_activity ;
	//	private String briefContent_activity ;
	//	private int start_time_activity ;
	//	private int end_time_activity ;
	//	private String venue_activity ;
	//	private JSONArray activityTags;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_details);
		setupView();
		initData() ;
		setlisteners();
	}
	private void setupView() {
		mInflater = LayoutInflater.from(this) ;
		getActivityData = this.getIntent().getStringExtra("new") ;
		options = new DisplayImageOptions.Builder()
		.showImageOnLoading(R.drawable.ic_launcher)
		.showImageForEmptyUri(R.drawable.ic_launcher)
		.showImageOnFail(R.drawable.ic_launcher).cacheInMemory(true)
		.cacheOnDisc(true).considerExifParams(true)
		.bitmapConfig(Bitmap.Config.RGB_565).build();

		register_button=(Button)findViewById(R.id.regist_bt);
		activity_detail_backbutton=(ImageView)findViewById(R.id.activity_detail_backbutton);
		activity_detail_imageView = (ImageView) this.findViewById(R.id.activity_detail_imageView) ;
		activity_detail_title = (TextView) this.findViewById(R.id.activity_detail_title) ;
		activity_detail_content = (TextView) this.findViewById(R.id.activity_detail_content) ;
		activity_detail_time = (TextView) this.findViewById(R.id.activity_detail_time) ;
		activity_detail_date = (TextView) this.findViewById(R.id.activity_detail_date) ;
		activity_detail_venue = (TextView) this.findViewById(R.id.activity_detail_venue) ;
		activity_detail_flowlayout = (CustomFlowLayout) this.findViewById(R.id.activity_detail_flowlayout) ;
	}

	private void initData() {
		try {
			JSONObject jsonObject = new JSONObject(getActivityData) ;
			ImageLoader.getInstance().displayImage(jsonObject.optString("banner"), activity_detail_imageView,options) ;
			activity_detail_title.setText(jsonObject.optString("subject")) ;
			activity_detail_content.setText(jsonObject.optString("briefContent")) ;
			activity_detail_time.setText(String.format(jsonObject.optInt("startTime")+"%s", "-"+jsonObject.optInt("endTime"))) ;
			activity_detail_date.setText("2014-45-98") ;
			activity_detail_venue.setText(jsonObject.getString("venue")) ;

			JSONArray flowLayoutArray = jsonObject.optJSONArray("tags") ;
			activity_detail_flowlayout.removeAllViews() ;

			for (int i = 0; i < flowLayoutArray.length(); i++) {
				TextView tv = (TextView) mInflater.inflate(R.layout.tv,activity_detail_flowlayout, false);
				String tagName = flowLayoutArray.optJSONObject(i).optString("name") ;
				//取得缩写名字
				String abbreName = CommonUtils.getAbbreviation(tagName) ;
				tv.setText(abbreName) ;
				CommonUtils.setColor(abbreName,tv,ListCampDetailsActivity.this) ;
				activity_detail_flowlayout.addView(tv) ;
			}

		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	private void setlisteners() {
		register_button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {

				Intent  intent=new Intent(ListCampDetailsActivity.this,RegisterActivity.class);
				intent.putExtra("application_list", getActivityData);
				startActivity(intent);

			}
		});

		activity_detail_backbutton.setOnClickListener(backOnclickListener) ;
	}

	private View.OnClickListener backOnclickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			finish() ;
		}
	};

	/***
	 * subject_activity=jsonObject.getString("subject");
				briefContent_activity=jsonObject.getString("briefContent");
				start_time_activity=jsonObject.getInt("startTime");
				end_time_activity=jsonObject.getInt("endTime");
				venue_activity=jsonObject.getString("venue");

				activityTags = jsonObject.optJSONArray("tags") ;
	 */
}
